/**
 * python gen: Thu May 27 10:27:58 2021
 */
import { apiService, basUrl, sysdeptService } from "@/api/apiService";

export default {
  name: "sysdeptHome",
  data() {
    return {
      options: [],
      searchForm: {
        // name: "",
        CHECK_RESULT: "",
        sort: ""
      },
      multipleSelection: [],
      tableData: [],
      currentPage: 1,
      totalCount: 0,
      PageSize: this.pageSizes[0],

      dialogFormVisible: false,
      ruleForm: {
        CHECK_ID: "",

        name: "",

        deptSort: "",

        pid: "",

        enabled: ""
      },
      rules: {
        CHECK_ID: [
          // { required: true, message: "不能为空", trigger: "blur" }
        ],

        name: [{ required: true, message: "不能为空", trigger: "blur" }],

        deptSort: [
          // { required: true, message: "不能为空", trigger: "blur" }
        ],

        pid: [
          // { required: true, message: "不能为空", trigger: "blur" }
        ],

        enabled: [
          // { required: true, message: "不能为空", trigger: "blur" }
        ]
      }
    };
  },
  mounted() {
    // do sth
    // this.getList(true);
    this.loadTree();
    this.loadAll();
  },

  methods: {
    //todo

    // 新增， 修改 【上级部门】
    loadAll() {
      sysdeptService.all().then(res => {
        let { data } = res;
        this.options = data;
      });
    },

    loadTree() {
      sysdeptService.tree().then(res => {
        let { data } = res;
        this.tableData = data;
      });
    },

    handleSortChange({ prop, order }) {
      // console.log(column, prop, order);
      if (!order) {
        return;
      }
      if ("descending" === order) {
        this.searchForm.sort = `${prop},desc`;
      }
      if ("ascending" === order) {
        this.searchForm.sort = `${prop}`;
      }

      this.getList();
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      // 改变每页显示的条数
      this.PageSize = val;
      // 注意：在改变每页显示的条数时，要将页码显示到第一页
      this.currentPage = 1;
      this.getList(true);
    },
    // 显示第几页
    handleCurrentChange(val) {
      // 改变默认的页数
      this.currentPage = val;
      this.getList();
    },

    resetForm() {
      this.$refs["searchForm"].resetFields();
      this.getList();
    },
    // getList(flag) {
    //   flag && (this.currentPage = 1);
    //   let params = {
    //     size: this.PageSize,
    //     page: this.currentPage - 1,
    //     ...this.searchForm
    //   };
    //   apiService.queryEachTable(tablename, params).then(res => {
    //     let {
    //       data: { total, records }
    //     } = res;
    //     this.totalCount = total;
    //     this.tableData = records;
    //   });
    // },

    // to 新增
    goAdd() {
      Object.keys(this.ruleForm).forEach(key => (this.ruleForm[key] = ""));
      this.dialogFormVisible = true;
      this.ruleForm.enabled = true;
      //   this.$refs["ruleForm"].resetFields();
      //   this.$router.push({ name: "SYSDEPT-edit", params: { id: "" } });
      //   this.kjAddMenuTab({
      //     label: "详细信息",
      //     name: "SYSDEPT-edit",
      //     path: "/kj/east/SYSDEPT-edit",
      //     params: { id: "3333" },
      //   });
    },

    // to 编辑
    goUpdate(item) {
      // let item = this.multipleSelection[0];
      let { oid, pid, subCount, name, deptSort, enabled } = item;
      this.ruleForm = JSON.parse(
        JSON.stringify({
          oid,
          pid,
          subCount,

          name,
          deptSort,
          enabled
        })
      );
      this.dialogFormVisible = true;
    },

    goCopy(item) {
      item.oid = "";
      this.goUpdate(item);
    },

    // 新增，修改 后， 点击[保存]
    doSave() {
      this.$refs["ruleForm"].validate(valid => {
        if (valid) {
          sysdeptService.postItem(this.ruleForm).then(() => {
            this.dialogFormVisible = false;
            this.loadTree();
          });
        }
      });
    },

    // to delete
    goDelete(item) {
      this.$confirm("确定要删除数据吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.doDelete(item);
        })
        .catch(() => {});
    },

    doDelete(item) {
      // let ids = this.multipleSelection.map(it => it.CHECK_ID);
      let params = { ids: item.oid };
      sysdeptService.deleteItem(params).then(() => {
        this.loadTree();
      });
    },

    // // to check
    // doCheck() {
    //   let params = { tables: "SYSDEPT" };
    //   apiService.checkItem(params).then(res => {
    //     let { total, errors } = res;
    //     this.$message({
    //       message: `总扫描记录行数: ${total}, 发现错误数: ${errors} `,
    //       type: "success"
    //     });
    //     this.getList();
    //   });
    // },

    // to import
    goImport() {},
    // to export
    goExport() {
      // let params = {
      //   tableCode: "SYSDEPT",
      //   desensitize: true
      // };
      // // apiService.exportItem(params);
      // window.open(
      //   `${basUrl}/collect/packet/export?tableCode=${
      //     params.tableCode
      //   }&desensitize=${params.desensitize}`
      // );
    }
  }
};
